<?php
/**
 * 后台菜单
 * User 1874
 */

namespace app\adminplatform\controller; 
use app\adminplatform\model\BackMenu as BackMenuModel; 
use app\adminplatform\model\AuthGroup; 
use app\adminplatform\model\Log;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Request;
use think\facade\View; 
use think\facade\Session; 

class BackMenu extends Initial{ 
    /**
     * backMenu分页列表
     */
    public function backMenuList(){
        $userData = Session::get('userData');
        $map = [];
        //查询数据
        $info = findChildLevel(BackMenuModel::getBackMenuList($map)->toArray(), "bm_pid", "bm_id");
        //权限验证
        $rules = ['BackMenu/backMenuOperate/operate/add', 'BackMenu/backMenuOperate/operate/edit','BackMenu/backMenuDel'];
        $check = AuthGroup::checkMethodArray($userData, $rules);

        View::assign('info', $info);
        View::assign('check',$check);
        return View::fetch('backMenuList');
    }
    /**
     * 后台菜单信息操作
     */
    public function backMenuOperate(string $operate){
        if($operate == "add"){          /**新增*/
            if(Request::isPost()) return $this->backMenuAdd();
        }elseif($operate == "edit"){    /**编辑*/
            if(Request::isPost()) return $this->backMenuEdit();
        }else{
            return alertDemo("请求错误");
        }
        $id = input('id');
        //是否接收到父id
        if(input("pid")){
            $parent = BackMenuModel::getBackMenuDetail(["bm_id"=>input("pid")])->bm_title; //查询父级名称
        }else{
            $parent = "顶级";
        }
        $info = BackMenuModel::getBackMenuDetail(['bm_id'=>$id]); //查询菜单数据
        View::assign('info', $info);
        View::assign('parent',$parent);
        return View::fetch('backMenuOperate');
    }
    /**
     * backMenu新增
     */
    protected function backMenuAdd(){
        //接收数据
        $map = input();
        //空值验证
        checkField($map, ["bm_id", "bm_icon", "bm_url"]);
        //插入数据
        $info = BackMenuModel::create($map);
        if($info){
            Log::logAdd(Session::get('userData.ad_id'), '新增系统菜单'); //写入日志
            $this->backMenuCacheDel();
            $data = ['info'=>'新增成功','status'=>1];
        }else{
            $data = ['info'=>'新增失败','status'=>0];
        }
        return json($data);
    }
    /**
     * backMenu编辑
     */
    protected function backMenuEdit(){
        //接收数据
        $map = input();
        //空值验证
        checkField($map, ["bm_icon", "bm_url"]);
        //插入数据
        $info = BackMenuModel::update($map, ['bm_id'=>$map["bm_id"]]);
        if($info){
            //写入日志
            Log::logAdd(Session::get('userData.ad_id'), '编辑系统菜单');
            $this->backMenuCacheDel();
            $data = ['info'=>'编辑成功','status'=>1];
        }else{
            $data = ['info'=>'编辑无变化','status'=>0];
        }
        return json($data);
    }
    /**
     * backMenu删除
     */
    public function backMenuDel(){
        if(Request::isPost()){
            $id = input('id');
            //删除数据
            $info = BackMenuModel::where('bm_id',$id)->delete();
            if($info){
                //写入日志
                Log::logAdd(Session::get('userData.ad_id'), '删除系统菜单');
                $this->backMenuCacheDel();
                $data = ['info'=>'删除成功','status'=>1];
            }else{
                $data = ['info'=>'删除失败','status'=>0];
            }
        }else{
            $data = ['info'=>'请求错误','status'=>0];
        }
        return json($data);
    }
    /**
     * 删除菜单缓存
     */
    protected function backMenuCacheDel(){
        Cache::delete(Config::get("app.admin_menu"));
    }
}